package com.millstein.eduservice.controller;


import com.millstein.commonutils.Result;
import com.millstein.eduservice.entity.EduChapter;
import com.millstein.eduservice.entity.chapter.ChapterVo;
import com.millstein.eduservice.service.EduChapterService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 课程 前端控制器
 * </p>
 *
 * @author testjava
 * @since 2020-07-15
 */
@RestController
@RequestMapping("/eduservice/chapter")
@CrossOrigin
public class EduChapterController {

    @Autowired
    private EduChapterService eduChapterService;

    /**
     * 根据课程id查询章节信息
     * @param courseId
     * @return
     */
    @GetMapping("getChapterVideo/{courseId}")
    public Result getChapterVideo(@PathVariable("courseId") String courseId) {
        List<ChapterVo> list = eduChapterService.getChapterVideo(courseId);
        return Result.ok().data("list", list);
    }

    /**
     * 新增章节
     * @param eduChapter
     * @return
     */
    @PostMapping("addChapter")
    public Result addChapter(@RequestBody EduChapter eduChapter) {
        eduChapterService.save(eduChapter);
        return Result.ok();
    }

    /**
     * 根据id获取章节信息
     * @param chapterId
     * @return
     */
    @GetMapping("getChapterInfo/{chapterId}")
    public Result getChapterInfo(@PathVariable("chapterId") String chapterId) {
        EduChapter eduChapter = eduChapterService.getById(chapterId);
        return Result.ok().data("chapter", eduChapter);
    }

    /**
     * 更新章节
     * @param eduChapter
     * @return
     */
    @PostMapping("updateChapter")
    public Result updateChapter(@RequestBody EduChapter eduChapter) {
        eduChapterService.updateById(eduChapter);
        return Result.ok();
    }

    /**
     * 根据id删除章节
     * @param chapterId
     * @return
     */
        @DeleteMapping("{chapterId}")
    public Result deleteChapter(@PathVariable("chapterId") String chapterId) {
        eduChapterService.deleteChapter(chapterId);
        return Result.ok();
    }

}

